<?
    include('inc_config.php');
    set_time_limit(24*86400);
    ob_clean();
    ?>
    <meta http-equiv="Content-Type" content="text/html"; charset="utf-8">
    <?
    $curl       = new Curl_HTTP_Client();
    $db_link    =  new db_query("SELECT *
                                 FROM links
                                 STRAIGHT_JOIN laws
                                 ON(law_id = lin_law_id)
                                 WHERE lin_status = 0
                                 ORDER BY RAND()
                                 LIMIT 1");
    $list_link  = mysql_fetch_assoc($db_link->result);
    unset($db_link);
    if($list_link){
        $link_id            = $list_link['lin_id'];
        $cat_id             = $list_link['law_cat_id'];
        $link_url           = $list_link['lin_value'];
        $domain_id          = $list_link['law_dom_id'];
        if($domain_id  == '10'){
            $link_url       = 'www.'.$link_url;
        }
        $cond_title         = $list_link['law_title'];
        $coud_image         = $list_link['law_image'];
        @$coud_teaser       = $list_link['law_teaser'];
        $cond_description   = $list_link['law_details'];
        $back_link          = $cond_description.' a';
        $cond_tags_remove   = $list_link['law_tag_remove'];
        $cond_tags_get      = $list_link['law_tag_get'];
        $coud_author        = $list_link['law_author'];
        $path               = $cat_id;
        $html_details       = $curl->fetch_url($link_url);
        if(!$html_details){
         $html_details       =  CURLCONTENT($link_url,$link_url);
        }
        $html_details       = str_get_html($html_details);
        $title              = $html_details->find($cond_title,0);
        @$title             = $title->innertext;
        @$title_remover     = $title->outertext;
        @$title             = strip_tags(removeHTML($title));
        $teaser = '';
        $image = array();
        foreach ($html_details->find($cond_description) as $description) {
            $description = $description->innertext;
        }
        $check_img = 0;
        foreach($html_details->find($coud_image) as $value){
            if($value->src){
                $image[] = $value->src;
                $check_img = 1;
            }
        }
        @$description = str_replace($title_remover,'',$description);
        if($check_img === 1 && @$description ){
            foreach ($html_details->find($coud_teaser) as $teaser) {
                @$teaser        = $teaser->innertext;
                @$teaser        = explode('<br>',$teaser);
                @$teaser        = $teaser[0];
                $teaser         = strip_tags($teaser);
                @$teaser        = str_replace('(Dân trí) - ','',$teaser);
                @$teaser        = str_replace('(Dân trí)- ','',$teaser);
                @$teaser        = str_replace('Xahoi - ','',$teaser);
                @$teaser        = trim($teaser);
            }
            foreach($html_details->find($cond_tags_remove) as $value){
                @$tags = $value->outertext;
                @$description = str_replace($tags,'',$description);
            }
            $taggets = '';
            foreach ($html_details->find($cond_tags_get) as $tagget) {
                $tagsget   = trim($tagget->innertext);
                $tagsget   = strip_tags($tagsget);
                @$taggets .= $tagsget.','; 
            }
            
            foreach ($html_details->find($back_link) as $key => $del_backlink) {
                $description = str_replace($del_backlink,$del_backlink->innertext, $description);
            }
            $new_picture = "";
            foreach ($image as $value) {           
              //@$imagec     = file_get_contents($value->src);
              //@$name_image  = generate_name(cut_string_url($value->src));
//              $imagel     = '/pictures/news/'.$name_image;
               $url = getimagesize($value);
               if(is_array($url))     
               {
                   $new_picture    =   $value;
                   break;
               }
                //@file_put_contents('..'.$imagel,$imagec);
//              $imglast = resize_image('..'.$imagel,$name_image,'120px','100px',200,"");
//              echo $imglast;
//              die;
            }
            foreach($html_details->find('img[src=http://dantri4.vcmedia.vn/Ic3EyFHpPWFvMJOJFocc/Image/2013/10/460-x-120-px-0b5da.gif]') as $remove_img){
                @$remove_img = $remove_img->outertext;
            }
            if(@$remove_img){
                $description = str_replace($remove_img,'',$description);
            }
            @$input = $html_details->find('input[type=hidden]');
            foreach ($input as $value) {
                @$description    = str_replace($value,'',$description);
            }
            $description = str_replace('<p style="MARGIN: 0in 0in 0pt" class=MsoNormal><o:p>&nbsp;</o:p></p>','',$description);
            $description = removeScript($description);
            //$a = new HtmlFixer();
//            $description = $a->getFixedHtml($description);
//            echo $description;
//            die;
            $title       = str_replace('(Soha.vn) - ','',$title);
            $title_md5 = md5($title);
            $time   = time();
            $msgerror = '';
            $myform = new generate_form();
            $myform->addTable('news');
            $myform->removeHTML(0);
            $myform->add('new_title','title',0,1,"",0,"",1,'Trùng nội dung tin');
            $myform->add('new_title_md5','title_md5',0,1,'',0,"",0);
            $myform->add('new_picture','new_picture',0,1,'',0);
            $myform->add('new_teaser','teaser',0,1,"",0,"",0);
            $myform->add('new_tags','taggets',0,1,'',0);
            $myform->add('new_cat_id','cat_id',1,1,0,0,'',0);
            $myform->add('new_detail','description',0,1,"",0,"",0);
            $myform->add('new_date','time',1,1,0,0,'',0);
            $myform->add('new_link','link_url',0,1,'',0,'',0);
            $msgerror  .= $myform->checkdata();
            if($msgerror == ""){
                //post lên hot 
                $data   = array('title'         =>$title,
                                'new_picture'   =>$new_picture,
                                'new_teaser'    =>$teaser,
                                'cat_id'        =>$cat_id,
                                'new_tags'      =>$taggets,
                                'new_detail'    =>$description,
                                'new_link'      =>$link_url
                                );
                $data = http_build_query($data);
                //$data = "title=".$title."&new_picture=".$new_picture."&new_teaser=".$teaser."&new_tags=".$taggets."&new_cat_id=".$cat_id."&new_detail=".base64_encode($description)."&new_link=".$link_url;
                $post_data = POSTDATAUP($data);
                $db_update      =   new db_execute("UPDATE links 
                                                    SET lin_status = 1
                                                    WHERE lin_id = ".$link_id);
                unset($db_update);
                //echo $myform->generate_insert_SQL();
//                die;
                $db_insert = new db_execute($myform->generate_insert_SQL());
                if($post_data =='success'){
                  echo 'Lấy thành công tin: <a href='.$link_url.' target="_blank">'.$title.'</a>';
                  echo $description;
                  unset($db_insert);
                }else{
                  echo 'Đã trùng tin';
                }
            }else{
                $db_update      =   new db_execute("UPDATE links 
                                                    SET lin_status = 1
                                                    WHERE lin_id = ".$link_id);
                unset($db_update);
                echo 'Đã lấy tin : <a href='.'/'.$link_url.' target="_blank">'.$title.'</a> rồi';
            }
        }else{
            echo 'Không lấy tin <a href='.$link_url.' target="_blank">'.$title.'</a>';
            $db_update      =   new db_execute("UPDATE links 
                                                    SET lin_status = 1
                                                    WHERE lin_id = ".$link_id);
            unset($db_update);
        }
    }else{
       echo 'Hết tin để lấy';
    }
?>
<meta http-equiv="refresh" content="6; url="get_new_details.php" />